package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022/8/2.
 * 作业计划管理
 */
@Component
public class JobplanHandler  extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;


    //统计作业计划里边，人数选择，人数之和
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("gq_joblabor".equals(objName)){
            String jpnum = MapUtils.getString(data, "jpnum");
           // String frontid =  MapUtils.getString(data, "frontid");
            List<Map<String, Object>> mapList = handlerService.findBySql(" select sum(quantity) as quantitys from gq_joblabor where jpnum ='"+jpnum+"'");


            if(mapList.size()>0){
                Integer quantitys = Integer.valueOf(mapList.get(0).get("quantitys").toString());
                //quantitys = quantitys+Integer.valueOf(frontid);
                String sql = "update gq_jobplan set peoplequantity ="+quantitys+" where jpnum = '"+jpnum+"'";
                handlerService.updateBySql(sql);
            }

        }
    }
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {

        if("gq_joblabor".equals(objName)){
            //通过id查询子表信息，获取管理主编的jpnum值，和子表人数信息
            String sql = "select jpnum,quantity from gq_joblabor where id = "+id;
            List<Map<String, Object>> mapList = handlerService.findBySql(sql);
            if(mapList.size()>0){
                String jpnum = mapList.get(0).get("jpnum").toString();
                Integer quantity = Integer.valueOf(mapList.get(0).get("quantity").toString());
                String sql2 = "select  peoplequantity from gq_jobplan where jpnum = '"+jpnum+"'";
                //通过主表编号，查询主表人数信息
                mapList = handlerService.findBySql(sql2);
                if(mapList.size()>0){
                    Integer peoplequantity = Integer.valueOf(mapList.get(0).get("peoplequantity").toString());
                    peoplequantity= peoplequantity-quantity;
                    //主表人数信息减去子表人数信息，然后更新主表人数信息
                    String sql3 = "update gq_jobplan set peoplequantity ="+peoplequantity+" where jpnum = '"+jpnum+"'";
                    handlerService.updateBySql(sql3);
                }

            }


        }
    }


}
